﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace SalienceEngineDemo
{
    
    [Serializable()]
    public class Settings : ISerializable
    {
        public SortedList<string, string> settings;
        public string settingsPath;

        public Settings(string settingtype, string sDataPath, string sInstallRoot, bool bShortMode, string sUserPath = null)
        {
            string sUser = sUserPath == null ? sDataPath + "\\user" : sUserPath;
            settings = new SortedList<string, string>();
            if (bShortMode)
            {
                    // Set up the default settings for Twitter
                settings.Add("TextThreshold", "40");
                settings.Add("ListsandTables", "False");
                settings.Add("OneSentence", "True");
                settings.Add("ComplexStems", "True");
                settings.Add("UsePolarityModel", "True");
            }
            else
            {
                settings.Add("TextThreshold", "80");
                settings.Add("ListsandTables", "True");
                settings.Add("OneSentence", "False");
                settings.Add("ComplexStems", "False");
                settings.Add("UsePolarityModel", "False");
            }
            settings.Add("Name", settingtype);
            settings.Add("DataDirectory", sDataPath);
            settings.Add("UserDirectory", sUser);
            settings.Add("TextTimeout", "0");
            settings.Add("FailLongSentences", "True");
            settings.Add("HTMLContent", "False");
            settings.Add("DefaultQueryTopicFile", sDataPath + "\\salience\\tags\\sampletags.dat");
            settings.Add("DefaultConceptTopicFile", sDataPath + "\\salience\\concepts\\example.dat");
            settings.Add("SummaryLength", "3");
            // Salience 5.1.1 introduced binary HSD file for performance, preferred over HSD if exists
            if (File.Exists(sDataPath + "\\salience\\sentiment\\general.hsdx"))
                settings.Add("DefaultHSDFile", sDataPath + "\\salience\\sentiment\\general.hsdx");
            else
                settings.Add("DefaultHSDFile", sDataPath + "\\salience\\sentiment\\general.hsd");
            settings.Add("EntityThreshold", "55");
            settings.Add("EntitySummaryLength", "3");
            settings.Add("MaxConceptHits", "5");
            settings.Add("MinConceptScore", "0.45");
            settings.Add("ConceptSlop", "0.8");
            settings.Add("DocumentSemantics", "False");
            settings.Add("SharedMemory", "False");
            settings.Add("FlattenAllUppercase", "False");
            settings.Add("ConceptTopicWindowSize", "0");
            settings.Add("ConceptTopicWindowJump", "1");
            settings.Add("NongrammaticalTopics", "True");
            settings.Add("AnaphoraResolution", "True");
            settings.Add("ModelSensitivity", "1000000");
            settings.Add("EntityOverlap", "True");
            settings.Add("UserEntityList", "");
            settings.Add("OverlapSentimentThemes", "False");
            settings.Add("EntityTopics", "True");
            settings.Add("AddSentimentDictionary", "");
            settings.Add("ReturnAllPossibleSentiment", "False");
            settings.Add("EmphaticMultiplier", "2.0");
            settings.Add("SuperlativeMultiplier", "1.25");
            settings.Add("TopicStemming", "True");
            settings.Add("ThemeTopics", "True");
            settings.Add("RequiredEntities", "");
            settings.Add("LogPath", "");
            settings.Add("LogStartup", "False");
            settings.Add("ExplainCategories", "False");
            settingsPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Lexalytics\\Salience Demo\\" + settingtype + "settings.bin";
        }

        public Settings(SerializationInfo info, StreamingContext ctxt)
        {
            this.settings = (SortedList<string, string>)info.GetValue("settings", typeof(SortedList<string, string>));
            this.settingsPath = (string)info.GetValue("settingsPath", typeof(string));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("settings", this.settings);
            info.AddValue("settingsPath", this.settingsPath);
        }

        public void Save()
        {
            new SettingsSerializer().Serialize(this.settingsPath, this);
        }

    } 
}
